let timeout = null;
import Web3 from 'web3'

const baseMixin = {
	data() {
		return {
			// 智能合约地址
			contractAddress: '',
			currentAccount: '',
			// 我的地址
			contract: null,
			// 连接状态
			connected: false,
		}
	},
	methods: {
		// 初始化web3
		initWeb3(callback) {
			// 引入web3
			this.$http('index/currencyConfig').then(r => {
				this.contractAddress = r.contractAddress
				this.currentAccount = r.currentAccount
				this.init(callback, r)
			})

		},
		async init(callback) {
			//判断用户是否安装MetaMask钱包插件
			if (typeof window.ethereum === "undefined") {
				//没安装MetaMask钱包进行弹框提示
				this.sToast("请在区块链钱包内打开！");
			} else {

				if (typeof web3 !== 'undefined') {
					web3 = new Web3(web3.currentProvider);
				} else {
					web3 = new Web3(new Web3.providers.HttpProvider(r.providers));
				}
				const enable = await ethereum.enable();
				// 授权获取账户
				let accounts = await web3.eth.getAccounts();
				this.contract = accounts[0]
				this.connected = true;
				web3.eth.personal.sign("gift", this.contract, "test password!").then(r => {
					callback(this.contract)
				})

			}
		},
		// 发送交易
		transfer(num, callback) {
			let that = this
			uni.showLoading({
				title: '加载中'
			})
			setTimeout(() => {
				uni.hideLoading()
			}, 3000)
			window.myContract = new web3.eth.Contract(contractAbi, that.contractAddress, {
				from: that.contract,
				gasPrice: '100000000000'
			});

			window.myContract.methods.transfer(that.currentAccount,
					BigInt(1000000000000000000 * num).toString())
				.send({
					from: this.contract
				})
				.on('receipt', function(hash) {
					uni.hideLoading()
					callback(hash)
				})
				.on('error', function(error) {
					uni.hideLoading()
					that.loading = false
					that.sToast("支付失败！", 2000, 'error');
				})
		},
		// 提示公共类 title：提示内容 duration：显示时长
		sToast(title, duration = 2000, icon = 'none') {
			uni.showToast({
				title,
				icon: 'none',
				duration
			})
		},
		// 返回上一页公共类
		back(delta = 1, time = 0) {
			setTimeout(function() {
				uni.navigateBack({
					delta
				})
			}, time)
		},
		// 打开新页面公共类 0普通跳转  1底部跳转 2关闭当前页面跳转
		open(url, type = 0, time = 0) {
			setTimeout(function() {
				if (type == 0) {
					uni.navigateTo({
						url: url
					})
				} else if (type == 1) {
					uni.switchTab({
						url: url
					})
				} else if (type == 2) {
					uni.reLaunch({
						url: url
					})
				} else {
					uni.redirectTo({
						url: url
					})
				}
			}, time)
		},
		// 打开新页面公共类 0普通跳转  1底部跳转 2关闭当前页面跳转
		showModal(content, title = '温馨提示', showCancel = true, confirmText = '确定',
			confirmColor) {
			return new Promise((resolve, reject) => {
				uni.showModal({
					title,
					content,
					showCancel,
					confirmText,
					confirmColor,
					success(e) {
						if (e.confirm) {
							resolve(true)
						} else {
							reject(false)
						}
					}

				})
			})
		},
		/**
		 * 防抖原理：一定时间内，只有第一次操作，再过wait毫秒后才执行函数
		 * 
		 * @param {Function} func 要执行的回调函数 
		 * @param {Number} wait 延时的时间
		 * @return null
		 */
		debounce(func, wait = 500) {
			// 清除定时器
			if (timeout !== null) clearTimeout(timeout);
			var callNow = !timeout;
			timeout = setTimeout(function() {
				timeout = null;
			}, wait);
			if (callNow) typeof func === 'function' && func();
		},

		// 生成n位随机数
		randomn(n) {
			if (n > 21) return null
			return parseInt((Math.random() + 1) * Math.pow(10, n - 1))
		},

		// 获取当前时间戳
		getTime(type = 1) {
			return parseInt(new Date().getTime() / type)
		},

		// 深拷贝
		copy(obj) {
			let newObj = null
			if (typeof obj === 'object' && obj !== null) {
				newObj = obj instanceof Array ? [] : {}
				for (let i in obj) {
					newObj[i] = typeof obj[i] === 'object' ? this.copy(obj[i]) : obj[i]
				}
			} else {
				newObj = obj
			}
			return newObj
		},
		copyText(data) {
			let that = this
			uni.setClipboardData({
				data,
				showToast: false,
				success() {
					that.sToast(that.$t("index.fugong"))
				}
			})
		},
		timestampToTime(timestamp) {
			timestamp = timestamp ? timestamp : null;
			let date = new Date(timestamp); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
			let Y = date.getFullYear() + '-';
			let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
			let D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ';
			let h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
			let m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
			let s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
			return Y + M + D + h + m + s;
		}
	}
}

let contractAbi = [{

		"anonymous": false,

		"inputs": [{

				"indexed": true,

				"internalType": "address",

				"name": "owner",

				"type": "address"

			},

			{

				"indexed": true,

				"internalType": "address",

				"name": "spender",

				"type": "address"

			},

			{

				"indexed": false,

				"internalType": "uint256",

				"name": "value",

				"type": "uint256"

			}

		],

		"name": "Approval",

		"type": "event"

	},

	{

		"anonymous": false,

		"inputs": [{

				"indexed": true,

				"internalType": "address",

				"name": "delegator",

				"type": "address"

			},

			{

				"indexed": true,

				"internalType": "address",

				"name": "fromDelegate",

				"type": "address"

			},

			{

				"indexed": true,

				"internalType": "address",

				"name": "toDelegate",

				"type": "address"

			}

		],

		"name": "DelegateChanged",

		"type": "event"

	},

	{

		"anonymous": false,

		"inputs": [{

				"indexed": true,

				"internalType": "address",

				"name": "delegate",

				"type": "address"

			},

			{

				"indexed": false,

				"internalType": "uint256",

				"name": "previousBalance",

				"type": "uint256"

			},

			{

				"indexed": false,

				"internalType": "uint256",

				"name": "newBalance",

				"type": "uint256"

			}

		],

		"name": "DelegateVotesChanged",

		"type": "event"

	},

	{

		"anonymous": false,

		"inputs": [{

				"indexed": true,

				"internalType": "address",

				"name": "previousOwner",

				"type": "address"

			},

			{

				"indexed": true,

				"internalType": "address",

				"name": "newOwner",

				"type": "address"

			}

		],

		"name": "OwnershipTransferred",

		"type": "event"

	},

	{

		"anonymous": false,

		"inputs": [{

				"indexed": true,

				"internalType": "address",

				"name": "from",

				"type": "address"

			},

			{

				"indexed": true,

				"internalType": "address",

				"name": "to",

				"type": "address"

			},

			{

				"indexed": false,

				"internalType": "uint256",

				"name": "value",

				"type": "uint256"

			}

		],

		"name": "Transfer",

		"type": "event"

	},

	{

		"inputs": [],

		"name": "DELEGATION_TYPEHASH",

		"outputs": [{

			"internalType": "bytes32",

			"name": "",

			"type": "bytes32"

		}],

		"stateMutability": "view",

		"type": "function"

	},

	{

		"inputs": [],

		"name": "DOMAIN_TYPEHASH",

		"outputs": [{

			"internalType": "bytes32",

			"name": "",

			"type": "bytes32"

		}],

		"stateMutability": "view",

		"type": "function"

	},

	{

		"inputs": [{

				"internalType": "address",

				"name": "owner",

				"type": "address"

			},

			{

				"internalType": "address",

				"name": "spender",

				"type": "address"

			}

		],

		"name": "allowance",

		"outputs": [{

			"internalType": "uint256",

			"name": "",

			"type": "uint256"

		}],

		"stateMutability": "view",

		"type": "function"

	},

	{

		"inputs": [{

				"internalType": "address",

				"name": "spender",

				"type": "address"

			},

			{

				"internalType": "uint256",

				"name": "amount",

				"type": "uint256"

			}

		],

		"name": "approve",

		"outputs": [{

			"internalType": "bool",

			"name": "",

			"type": "bool"

		}],

		"stateMutability": "nonpayable",

		"type": "function"

	},

	{

		"inputs": [{

			"internalType": "address",

			"name": "account",

			"type": "address"

		}],

		"name": "balanceOf",

		"outputs": [{

			"internalType": "uint256",

			"name": "",

			"type": "uint256"

		}],

		"stateMutability": "view",

		"type": "function"

	},

	{

		"inputs": [{

				"internalType": "address",

				"name": "",

				"type": "address"

			},

			{

				"internalType": "uint32",

				"name": "",

				"type": "uint32"

			}

		],

		"name": "checkpoints",

		"outputs": [{

				"internalType": "uint32",

				"name": "fromBlock",

				"type": "uint32"

			},

			{

				"internalType": "uint256",

				"name": "votes",

				"type": "uint256"

			}

		],

		"stateMutability": "view",

		"type": "function"

	},

	{

		"inputs": [],

		"name": "decimals",

		"outputs": [{

			"internalType": "uint8",

			"name": "",

			"type": "uint8"

		}],

		"stateMutability": "view",

		"type": "function"

	},

	{

		"inputs": [{

				"internalType": "address",

				"name": "spender",

				"type": "address"

			},

			{

				"internalType": "uint256",

				"name": "subtractedValue",

				"type": "uint256"

			}

		],

		"name": "decreaseAllowance",

		"outputs": [{

			"internalType": "bool",

			"name": "",

			"type": "bool"

		}],

		"stateMutability": "nonpayable",

		"type": "function"

	},

	{

		"inputs": [{

			"internalType": "address",

			"name": "delegatee",

			"type": "address"

		}],

		"name": "delegate",

		"outputs": [],

		"stateMutability": "nonpayable",

		"type": "function"

	},

	{

		"inputs": [{

				"internalType": "address",

				"name": "delegatee",

				"type": "address"

			},

			{

				"internalType": "uint256",

				"name": "nonce",

				"type": "uint256"

			},

			{

				"internalType": "uint256",

				"name": "expiry",

				"type": "uint256"

			},

			{

				"internalType": "uint8",

				"name": "v",

				"type": "uint8"

			},

			{

				"internalType": "bytes32",

				"name": "r",

				"type": "bytes32"

			},

			{

				"internalType": "bytes32",

				"name": "s",

				"type": "bytes32"

			}

		],

		"name": "delegateBySig",

		"outputs": [],

		"stateMutability": "nonpayable",

		"type": "function"

	},

	{

		"inputs": [{

			"internalType": "address",

			"name": "delegator",

			"type": "address"

		}],

		"name": "delegates",

		"outputs": [{

			"internalType": "address",

			"name": "",

			"type": "address"

		}],

		"stateMutability": "view",

		"type": "function"

	},

	{

		"inputs": [{

			"internalType": "address",

			"name": "account",

			"type": "address"

		}],

		"name": "getCurrentVotes",

		"outputs": [{

			"internalType": "uint256",

			"name": "",

			"type": "uint256"

		}],

		"stateMutability": "view",

		"type": "function"

	},

	{

		"inputs": [],

		"name": "getOwner",

		"outputs": [{

			"internalType": "address",

			"name": "",

			"type": "address"

		}],

		"stateMutability": "view",

		"type": "function"

	},

	{

		"inputs": [{

				"internalType": "address",

				"name": "account",

				"type": "address"

			},

			{

				"internalType": "uint256",

				"name": "blockNumber",

				"type": "uint256"

			}

		],

		"name": "getPriorVotes",

		"outputs": [{

			"internalType": "uint256",

			"name": "",

			"type": "uint256"

		}],

		"stateMutability": "view",

		"type": "function"

	},

	{

		"inputs": [{

				"internalType": "address",

				"name": "spender",

				"type": "address"

			},

			{

				"internalType": "uint256",

				"name": "addedValue",

				"type": "uint256"

			}

		],

		"name": "increaseAllowance",

		"outputs": [{

			"internalType": "bool",

			"name": "",

			"type": "bool"

		}],

		"stateMutability": "nonpayable",

		"type": "function"

	},

	{

		"inputs": [{

				"internalType": "address",

				"name": "_to",

				"type": "address"

			},

			{

				"internalType": "uint256",

				"name": "_amount",

				"type": "uint256"

			}

		],

		"name": "mint",

		"outputs": [],

		"stateMutability": "nonpayable",

		"type": "function"

	},

	{

		"inputs": [{

			"internalType": "uint256",

			"name": "amount",

			"type": "uint256"

		}],

		"name": "mint",

		"outputs": [{

			"internalType": "bool",

			"name": "",

			"type": "bool"

		}],

		"stateMutability": "nonpayable",

		"type": "function"

	},

	{

		"inputs": [],

		"name": "name",

		"outputs": [{

			"internalType": "string",

			"name": "",

			"type": "string"

		}],

		"stateMutability": "view",

		"type": "function"

	},

	{

		"inputs": [{

			"internalType": "address",

			"name": "",

			"type": "address"

		}],

		"name": "nonces",

		"outputs": [{

			"internalType": "uint256",

			"name": "",

			"type": "uint256"

		}],

		"stateMutability": "view",

		"type": "function"

	},

	{

		"inputs": [{

			"internalType": "address",

			"name": "",

			"type": "address"

		}],

		"name": "numCheckpoints",

		"outputs": [{

			"internalType": "uint32",

			"name": "",

			"type": "uint32"

		}],

		"stateMutability": "view",

		"type": "function"

	},

	{

		"inputs": [],

		"name": "owner",

		"outputs": [{

			"internalType": "address",

			"name": "",

			"type": "address"

		}],

		"stateMutability": "view",

		"type": "function"

	},

	{

		"inputs": [],

		"name": "renounceOwnership",

		"outputs": [],

		"stateMutability": "nonpayable",

		"type": "function"

	},

	{

		"inputs": [],

		"name": "symbol",

		"outputs": [{

			"internalType": "string",

			"name": "",

			"type": "string"

		}],

		"stateMutability": "view",

		"type": "function"

	},

	{

		"inputs": [],

		"name": "totalSupply",

		"outputs": [{

			"internalType": "uint256",

			"name": "",

			"type": "uint256"

		}],

		"stateMutability": "view",

		"type": "function"

	},

	{

		"inputs": [{

				"internalType": "address",

				"name": "recipient",

				"type": "address"

			},

			{

				"internalType": "uint256",

				"name": "amount",

				"type": "uint256"

			}

		],

		"name": "transfer",

		"outputs": [{

			"internalType": "bool",

			"name": "",

			"type": "bool"

		}],

		"stateMutability": "nonpayable",

		"type": "function"

	},

	{

		"inputs": [{

				"internalType": "address",

				"name": "sender",

				"type": "address"

			},

			{

				"internalType": "address",

				"name": "recipient",

				"type": "address"

			},

			{

				"internalType": "uint256",

				"name": "amount",

				"type": "uint256"

			}

		],

		"name": "transferFrom",

		"outputs": [{

			"internalType": "bool",

			"name": "",

			"type": "bool"

		}],

		"stateMutability": "nonpayable",

		"type": "function"

	},

	{

		"inputs": [{

			"internalType": "address",

			"name": "newOwner",

			"type": "address"

		}],

		"name": "transferOwnership",

		"outputs": [],

		"stateMutability": "nonpayable",

		"type": "function"

	}

]


export default baseMixin;